const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const models = require('../db/models');

app.use(express.json());
app.use(express.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));

console.log(models,111111)

//创建账号
app.use('/register',async (req,res,next)=>{
  try {
    let Obj =  req.body || req.query;
    console.log(Obj,'--register')
    let data = await models.users.create(Obj); //创建账号
    res.json({
      code:200,
      message:'创建成功',
      data
    })
  } catch (error) {
    next(error)
  }
  
})


//新增
app.use('/add',async (req,res,next)=>{
  try {
    let Obj =  req.body || req.query;
    console.log(Obj,'--add')
    let data = await models.todos.create(Obj); //创建账号
    res.json({
      code:200,
      message:'创建成功',
      data
    })
  } catch (error) {
    next(error)
  }
  
})
//查找
app.use('/find',async (req,res,next)=>{
  try {
    let {name,phone }=  req.body || req.query;
    let where = {
      name,phone
    }
    let limit ={
      offset:0,
      limit:10,
    }
    console.log(Obj,'--add')
    let data = await models.todos.findAndCountAll({where},limit); //查找的数据
    if(data){
      res.json({
        code:200,
        message:'创建成功',
        data
      })
    }else{
      res.json({
        message:`${name}或${phone}不存在`
      })
    }
    
  } catch (error) {
    next(error)
  }
  
})
//更新数据
app.use('/update',async (req,res,next)=>{
  try {
    let {goodsName,name,phone,email,status }=  req.body || req.query;
    let params = {
      name,phone,email,status
    }
    let where={
      goodsName
    }
    let limit ={
      offset:0,
      limit:10,
    }
    console.log(Obj,'--add')
    let data = await models.todos.update({where},params); //查找的数据
    if(data){
      res.json({
        code:200,
        message:'创建成功',
        data
      })
    }else{
      res.json({
        message:`${goodsName}不存在`
      })
    }
    
  } catch (error) {
    next(error)
  }
  
})
//删除后数据
app.use('/del',async (req,res,next)=>{
  try {
    let {goodsName }=  req.body || req.query;
    let where={
      goodsName
    }
    let limit ={
      offset:0,
      limit:10,
    }
    console.log(Obj,'--add')
    let data = await models.todos.destroy({where}); //查找的数据
    if(data){
      res.json({
        code:200,
        message:'删除成功',
        data
      })
    }else{
      res.json({
        message:`${goodsName}不存在`
      })
    }
    
  } catch (error) {
    next(error)
  }
  
})


//
app.use('/addgoods',async (req,res,next)=>{
  try {
    let Obj =  req.body || req.query;
    console.log(Obj,'===goods')
    let data = await models.list.create(Obj); //创建账号
    res.json({
      code:200,
      message:'创建成功',
      data
    })
  } catch (error) {
    next(error)
  }
  
})
app.use((err,req,res,next)=>{
  if(err){
    res.json({
      message:err.message
    })
  }else{
    res.status(500).json({
      message:'该服务停止'
    })
  }
})

app.listen(7000,()=>{
  console.log('listening running on locahost:7000')
})